package edu.task.snake.view;

import edu.task.snake.controller.GameEngine;

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.HeadlessException;


public class View extends JFrame {
    private int cellSizePixel;
    private GameEngine gameEngine;
    private javax.swing.JButton startButton;
    private javax.swing.JButton pauseButton;
    private javax.swing.JLabel scoreTextLabel;
    private javax.swing.JLabel scoreCountTextLabel;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel arenaPanel;

    public View(int cellSizePixel, GameEngine gameEngine) throws HeadlessException {
        this.cellSizePixel = cellSizePixel;
        this.gameEngine = gameEngine;

        setResizable(false);
        setTitle("Snake");
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBackground(Color.black);
        setFocusable(true);
        initComponent();
    }

    private void initComponent() {
        jPanel1 = new javax.swing.JPanel();
        startButton = new javax.swing.JButton();
        scoreTextLabel = new javax.swing.JLabel();
        scoreCountTextLabel = new javax.swing.JLabel();
        pauseButton = new javax.swing.JButton();
        arenaPanel = new ViewArena(cellSizePixel * gameEngine.getCellWidth(), cellSizePixel * gameEngine.getCellHeight());

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        startButton.setText("Start");
        pauseButton.setText("Pause");
        pauseButton.setVisible(false);

        scoreTextLabel.setText("Score:");
        scoreCountTextLabel.setText("0000");


        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addComponent(pauseButton, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(startButton)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(scoreTextLabel)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(scoreCountTextLabel)
                        )
        );
        jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(startButton)
                                .addComponent(scoreTextLabel)
                                .addComponent(scoreCountTextLabel)
                                .addComponent(pauseButton))
        );

        arenaPanel.setBackground(new java.awt.Color(51, 51, 51));
        arenaPanel.setFocusable(true);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(arenaPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(arenaPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }

    public void setGameEngine(GameEngine gameEngine) {
        this.gameEngine = gameEngine;
    }


    public JPanel getArenaPanel() {
        return arenaPanel;
    }


}
